package com.yunbao.project.basedata.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunbao.core.respones.Result;
import com.yunbao.core.respones.ResultBuilder;
import com.yunbao.core.respones.ResultEnum;
import com.yunbao.project.basedata.entity.Goods;
import com.yunbao.project.basedata.query.GoodsQuery;
import com.yunbao.project.basedata.service.IGoodsService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author 安能
 * @since 2019-09-16
 */
@RestController
@RequestMapping("goods")
public class GoodsController {


    @Resource
    IGoodsService goodsService;


    @PostMapping("insert")
    public Result insert(@RequestBody Goods goods) {
        goodsService.save(goods);
        return ResultBuilder.success(ResultEnum.ADD_SUCCESS);
    }

    @PostMapping("deleteById")
    public Result deleteById(Integer id) {

        goodsService.removeById(id);
        return ResultBuilder.success(ResultEnum.DELETE_SUCCESS);
    }

    @PostMapping("updateById")
    public Result updateById(@RequestBody Goods goods) {

        goodsService.updateById(goods);
        return ResultBuilder.success(ResultEnum.UPDATA_SUCCESS);
    }

    @PostMapping("getById")
    public Result getById(Integer id) {

        Goods goods = goodsService.getById(id);
        return ResultBuilder.success(goods);
    }

    /**
     * 获取账户列表
     *
     * @param goodsQuery
     * @return 账户列表
     */
    @PostMapping("getList")
    public Result getList(@RequestBody GoodsQuery goodsQuery) {
        IPage<Goods> page = goodsService.page(new Page<>(
                        goodsQuery.getCurrent(),
                        goodsQuery.getSize()
                )
        );
        return ResultBuilder.success(page);
    }

    @PostMapping("getListLike")
    public Result getListLike(@RequestBody GoodsQuery goodsQuery) {

        IPage<Goods> listLike = goodsService.getListLike(goodsQuery);
        return ResultBuilder.success(listLike);
    }
}
